package nix.jaxp;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class TestDom {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		//selectTags();
		//selectTag();
		//createTag();
		//modifyTag();
		//removeTag();
		listAll();
	}

	public static void selectTags() throws Exception {
		// 创建解析工厂实例
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory
				.newInstance();
		// 创建解析器实例
		DocumentBuilder builder = builderFactory.newDocumentBuilder();
		// 解析xml，返回Document
		Document document = builder.parse("src/nix/jaxp/person.xml");
		// 获得name元素
		NodeList list = document.getElementsByTagName("name");
		// 遍历集合
		for (int i = 0; i < list.getLength(); i++) {
			// 获得name元素的文本内容
			String text = list.item(i).getTextContent();
			System.out.println(text);
		}
	}

	public static void selectTag() throws Exception {
		// 创建解析工厂实例
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		// 创建解析器实例
		DocumentBuilder builder = builderFactory.newDocumentBuilder();
		// 解析xml，返回Document
		Document document = builder.parse("src/nix/jaxp/person.xml");
		// 获得name元素
		NodeList list = document.getElementsByTagName("name");
		// 遍历集合
		Node node = list.item(0);
		// 获得name元素的文本内容
		String text = node.getTextContent();
		System.out.println(text);
	}
	
	public static void createTag() throws Exception {
		// 创建解析工厂实例
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		// 创建解析器实例
		DocumentBuilder builder = builderFactory.newDocumentBuilder();
		// 解析xml，返回Document
		Document document = builder.parse("src/nix/jaxp/person.xml");
		// 获得第一个person元素
		Node node = document.getElementsByTagName("person").item(0);
		// 创建gender标签元素
		Element element = document.createElement("gender");
		// 创建文本节点
		Text text = document.createTextNode("male");
		// 将文本添加至gender元素
		element.appendChild(text);
		// 将gender元素添加至person元素
		node.appendChild(element);
		// 内存回写文件
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		transformer.transform(new DOMSource(document), new StreamResult("src/nix/jaxp/person.xml"));
	}
	
	public static void modifyTag() throws Exception {
		// 创建解析工厂实例
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		// 创建解析器实例
		DocumentBuilder builder = builderFactory.newDocumentBuilder();
		// 解析xml，返回Document
		Document document = builder.parse("src/nix/jaxp/person.xml");
		// 获得第一个person元素
		Node node = document.getElementsByTagName("gender").item(0);
		// 设置元素文本内容
		node.setTextContent("female");
		// 内存回写文件
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		transformer.transform(new DOMSource(document), new StreamResult("src/nix/jaxp/person.xml"));
	}
	
	public static void removeTag() throws Exception {
		// 创建解析工厂实例
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		// 创建解析器实例
		DocumentBuilder builder = builderFactory.newDocumentBuilder();
		// 解析xml，返回Document
		Document document = builder.parse("src/nix/jaxp/person.xml");
		// 获得第一个person元素
		Node node = document.getElementsByTagName("gender").item(0);
		// 获得父节点
		Node nodeParent = node.getParentNode();
		// 删除子节点
		nodeParent.removeChild(node);
		// 内存回写文件
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		transformer.transform(new DOMSource(document), new StreamResult("src/nix/jaxp/person.xml"));
	}
	
	public static void listAll() throws Exception {
		// 创建解析工厂实例
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		// 创建解析器实例
		DocumentBuilder builder = builderFactory.newDocumentBuilder();
		// 解析xml，返回Document
		Document document = builder.parse("src/nix/jaxp/person.xml");
		// 调用递归方法
		list1(document);
	}

	private static void list1(Node node) {
		// 判断是元素节点
		if (node.getNodeType() == Document.ELEMENT_NODE) {
			System.out.println(node.getNodeName());
		}
		// 获得直接子节点
		NodeList list = node.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			// 递归调用
			list1(list.item(i));
		}
	}
}
